package com.yanfan.energy.controller;

import cn.hutool.core.util.StrUtil;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.statist.DateStatistics;
import com.yanfan.energy.service.TotalEnergyService;
import com.yanfan.energy.service.WorkshopEnergyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @author caijinkang
 * @date 2022年09月28日 15:40
 */
@RestController
@RequestMapping("/energy")
@Api(tags = "能耗总分析")
public class TotalEnergyController {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final Logger log = LoggerFactory.getLogger(WorkshopEnergyController.class);
    @Resource
    private final WorkshopEnergyService workshopEnergyService;

    @Resource
    private final TotalEnergyService totalEnergyService;

    public TotalEnergyController(WorkshopEnergyService workshopEnergyService, TotalEnergyService totalEnergyService) {
        this.workshopEnergyService = workshopEnergyService;
        this.totalEnergyService = totalEnergyService;
    }

    @GetMapping("/total-energy/total-cost")
    @ApiOperation("车间总费用统计")
    public AjaxResult findTotalCost(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Long workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, Map<String, List<DateStatistics>>> statistics = totalEnergyService.findTotalCost(workshopId, floorId, buildingId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/total-energy/total-medium")
    @ApiOperation("能源介质总费用")
    public AjaxResult findTotalMedium(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = totalEnergyService.findTotalMedium(workshopId, floorId, buildingId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/total-energy/total-electricity")
    @ApiOperation("时段用电总量")
    public AjaxResult findTotalElectricity(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = workshopEnergyService.findTotalElectricity(workshopId, floorId, buildingId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/total-energy/total-workshop-cost-of")
    @ApiOperation("车间总费用占比")
    public AjaxResult findWorkshopCostOf(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Long workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, Map<String, List<DateStatistics>>> statistics = totalEnergyService.findWorkshopCostOf(workshopId, floorId, buildingId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/total-energy/total-medium-cost-of")
    @ApiOperation("能源介质总费用占比")
    public AjaxResult findMediumCostOf(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Integer workshopId, @ApiParam(name = "floorId", value = "楼层id") @RequestParam(required = false) Integer floorId, @ApiParam(name = "buildingId", value = "楼栋id") @RequestParam(required = false) Integer buildingId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = totalEnergyService.findMediumCostOf(workshopId, floorId, buildingId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

    @GetMapping("/total-energy/total-equipment-energy")
    @ApiOperation("设备总能耗")
    public AjaxResult findEquipmentEnergy(@ApiParam(name = "workshopId", value = "车间id") @RequestParam(required = false) Long workshopId, @ApiParam(name = "range", value = "日期格式") @RequestParam(required = false) String range, @ApiParam(name = "begin", value = "开始日期") @RequestParam(required = false) String begin, @ApiParam(name = "end", value = "结束日期") @RequestParam(required = false) String end, @ApiParam(name = "unit", value = "单位") @RequestParam Integer unit) {
        Map<String, List<DateStatistics>> statistics = totalEnergyService.findEquipmentEnergy(workshopId, range, LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : begin, formatter), LocalDate.parse(StrUtil.isNotBlank(range) ? formatter.format(LocalDate.now()) : end, formatter), unit);

        return AjaxResult.success("查询成功", statistics);
    }

}
